package com.teamsea.utils;

import java.util.Random;

import com.teamsea.pojo.User;

public class Users {

    // 生成随机值
    private static Random random = new Random();

    public static User[] generateRandomUserArray(Integer count) {

        // 生成一个随机的长度的User数组
        User[] users = new User[count];

        for (int i = 0; i < users.length; i++) {

            // 生成随机随机空值
            if (random.nextBoolean()) {
                users[i] = null;
                continue;
            }

            // 对不为空的user对象进行赋值
            // 随机生成属性值
            Integer id = random.nextBoolean() ? random.nextInt(100) : null;
            String name = random.nextBoolean() ? generateRandomString(5) : null;
            Integer age = random.nextBoolean() ? random.nextInt(80) + 18 : null;
            String phone = random.nextBoolean() ? "1" + generateRandomString(10, "0123456789") : null;
            String address = random.nextBoolean() ? generateRandomString(8) + " St." : null;

            users[i] = new User(id, name, age, phone, address);
        }

        return users;
    }

    // 生成随机字符串辅助方法
    private static String generateRandomString(int length) {
        return generateRandomString(length, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
    }

    private static String generateRandomString(int length, String allowedChars) {
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(allowedChars.charAt(random.nextInt(allowedChars.length())));
        }
        return sb.toString();
    }

}
